package com.yy.cloud;

import com.yy.cloud.config.RibbonIRuleConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RibbonClients(value = { // 开启ribbon的负载均衡，采用RibbonIRuleConfig.class这个类中声明的策略
        @RibbonClient(name = "goods-service", configuration = RibbonIRuleConfig.class)
})
public class LoadBalanceConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(LoadBalanceConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced // 实现负载均衡的功能，使用restTemplate就必须使用此注解，默认按照轮询机制
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}
